package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.vo.system.PageVO;
import com.goods.system.mapper.UserMapper;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class HealthServiceImpl implements HealthService {

    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    /*@Override
    public boolean findReport(String username) {
        return false;
    }*/

    @Override
    public void report(Health health) throws SystemException {
        String username = userService.info().getUsername();
        Example o = new Example(User.class);
        Example.Criteria criteria = o.createCriteria();
        criteria.andEqualTo("username",username);
        //设置health打卡对象的UserId
        User user = userMapper.selectOneByExample(o);

        health.setUserId(user.getId());
        healthMapper.insert(health);
    }

    @Override
    public PageVO<Health> history(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<Health> healthList = healthMapper.selectAll();

        PageInfo<Health> info = new PageInfo<>(healthList);

        return new PageVO<>(info.getTotal(), healthList);
    }
}
